<template>
  <el-dialog
    :before-close="beforeClose"
    :visible.sync="show"
    :title="title"
    width="30%"
    @close="close"
    @closed="closed"
  >
    <el-tree
      ref="tree"
      :data="permissionTreeList"
      show-checkbox
      node-key="id"
      :props="defaultProps"
    />
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import select from '@/list/select'
import { editRolePermissions, getRolePermissions } from '@/api/system'
export default {
  name: 'Range',
  mixins: [select],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      show: false,
      defaultProps: {
        children: 'menus',
        label: 'name'
      },
      permissionIds: []
    }
  },
  computed: {
    title() {
      return '配置权限'
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.show = val
        this.getPermissionTree()
        this.getData()
      }
    }
  },
  mounted() {
    this.show = this.visible
  },
  methods: {
    handleCheckedCitiesChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.cities.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length
    },
    handleChange(value) {
      console.log(value)
    },
    getData() {
      console.log('获取数据')
      getRolePermissions(this.data.id).then(res => {
        console.log('获取', res)
        const list = res.data.map(v => {
          return v.id
        })
        this.$refs.tree.setCheckedKeys(list)
      })
    },
    handleClose() {
      console.log('点击关闭')
      this.beforeClose()
    },
    handleSubmit() {
      const list = this.$refs.tree.getCheckedKeys()
      console.log('点击确定', list)
      editRolePermissions({ id: this.data.id, permissionIds: list }).then(res => {
        console.log('编辑保持', res)
      })
      this.$notify.success('保存成功')
      this.handleClose()
    },
    beforeClose() {
      console.log('关闭前的回调')
      this.show = false
      this.$emit('update:visible', false)
    },
    close() {
      console.log('关闭的回调')
    },
    closed() {
      console.log('关闭动画结束后的回调')
      this.$refs.tree.setCheckedKeys([])
    }
  }
}
</script>

<style scoped>

</style>
